<?php

namespace Apps\AppApi\Method\BeautyParlor;
use Apps\AppApi\Method\BaseMethod;
use Apps\Common\Models\BeautyParlorComment;
use Apps\Common\Libs\Keyword;
use Apps\Common\Libs\UploadPic;
use PhalconPlus\Api\Common\ResultStatus;
use Apps\Common\Models\CosQKeyword;
use Apps\Common\Models\CosOnlineAppointment;
use Apps\Common\Models\BeautyParlor;
use Apps\Common\Models\CosShop;
use Apps\Common\Models\Branch;
use Apps\Common\Libs\Jpush;
use Apps\Common\Libs\SmaUtil;

/**
 * 添加美容院评论
 * @package Apps\AppApi\Method\BeautyParlor
 */
class CommentAdd extends BaseMethod
{
    /**
     * @var \Apps\Common\Models\BeautyParlorComment
     */
    private $success = 0;
    private $id = 0;

    public function run()
    {
        /**
         * 验证用户权限
         */
        if(false == $this->verifyUserAuth()){
            return false;
        }  
        
        $bp_id = intval($this -> getDataItem('beauty_parlor_id',0));
        
        $comment_info = Keyword::Filter($this->getDataItem('content',''));
        $comment_info = trim($comment_info);
        //关键字替换
        $keyword = CosQKeyword::query() -> execute() -> toArray();
        foreach ($keyword as $k => $v){
        	 
        	$comment_info = str_replace($v['keyword'], '***', $comment_info);
        
        }
        
        //$type = intval($this -> getDataItem('type'));
        $type = $this -> getDataItem('type',0);
        //评论图片
        $up = new UploadPic();
        $comment_photos = json_encode($up->beautyParlorComment()); 
        
		if($bp_id > 0 && !empty($comment_info)) {
			// 查询最大楼层
			$floor = BeautyParlorComment::maximum([
					"column" => "comment_floor",
					"conditions" => "beauty_parlor_id = :id: and type = 0",
					"bind" => [
							"id" => $bp_id
					]
			]);
			

			$comment_info = base64_encode(serialize($comment_info));
			
			$bpc = new BeautyParlorComment();
			$bpc->user_id = $this->getUserAuth()->userId;
			$bpc->comment_floor = ($floor ? $floor : 0) + 1;
			$bpc->beauty_parlor_id=$bp_id;
			$bpc->comment_info=$comment_info;
			$bpc->comment_photos=$comment_photos;
			$bpc -> type = $type;
			if($bpc->save()){
				$this->id = $bpc->comment_id;
				$this->success = 1;
// 				return $this->setResult([
// 				    'id'=>$this->id,
// 				    'success'=>$this->success
// 				]);
			}else{
				return $this->databaseErrorLog($bpc);
			}
		} else {
			return $this->errorLog(ResultStatus::POST_BODY_PARAM_ERROR, "");
		}
		//评论成功，推送消息
		$branchId = BeautyParlor::findFirst("bp_id = $bp_id")->branch_id;
		if(empty($branchId)){//没有分店，不推送
			return $this->setResult([
					'id'=>$this->id,
					'success'=>$this->success
			]);
		}else{
			$smautil = new SmaUtil();
			$phoneNum = $smautil->getMobile($branchId);
			if (!empty($phoneNum['shop_mobile'])){
			    $param = array(
			        'type' => 7,
			        'ispushservice' => 0,
			        'phone' => $phoneNum,
			        'message_link_type' => 'beautyParlor',
			        'message_link_data' => $bp_id,
			        'content' => '有人对您的美容院进行了评论，快去看看吧！',
			        'shop_id' => 0
			    );
			    $push = new Jpush();
			    $rs = $push -> setval($param);
			    	
			    if(empty($rs)){
			        $rs = $push -> push();
			    }
			}
			if (!empty($phoneNum['boss_mobile'])){
			    $param = array(
			        'type' => 7,
			        'ispushservice' => 0,
			        'phone' => $phoneNum,
			        'message_link_type' => 'beautyParlor',
			        'message_link_data' => $bp_id,
			        'content' => '有人对您的美容院进行了评论，快去看看吧！',
			        'shop_id' => 0
			    );
			    $push = new Jpush();
			    $rs = $push -> setval($param);
			    
			    if(empty($rs)){
			        
			        $rs = $push -> push();
			    }
			}
			
		}
		
		return $this->setResult([
		    'id'=>$this->id,
		    'success'=>$this->success
		]);
    }
}